/****************************************************************************
**  Tite Interactive Text Engine (TiTE)
**  Copyright (C) 2010  Eisoft Group
**
**  This file is part of TiTE.
**
**  TiTE is free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation, either version 3 of the License, or
**  (at your option) any later version.
**
**  TiTE is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with TiTE.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

/*!
 * \file safe_delete.hpp
 *
 * \date 01/10/2011
 */

#ifndef TITE_CORE_SAFE_DELETE_HPP
#define TITE_CORE_SAFE_DELETE_HPP

#ifdef _TITE_RENDERER_D3D
#   include <Unknwn.h>
#endif

/*!
 * \namespace tite
 *
 * \brief The TiTE namespace.
 */
namespace tite
{
    /*!
     * \namespace core
     *
     * \brief The TiTE core namespace.
     */
    namespace core
    {
        /*!
         * \brief Releases the memory pointed to by the formal parameter in a safe
         *        manner.
         *
         * \param _ppT  Instance of T to deallocate.
         */
        template<typename T>
        inline void safe_delete(T ** _ppT)
        {
            if (*_ppT)
            {
                delete (*_ppT);
                *_ppT = 0;
            }
        }

#ifdef _TITE_RENDERER_D3D
        /*!
         * \brief Full template specialization of safe_delete<> for COM objects.
         *
         * \param _ppComPtr  COM Object to deallocate.
         */
        template<>
        inline void safe_delete<IUnknown>(IUnknown ** _ppComPtr)
        {
            if (*_ppComPtr)
            {
                (*_ppComPtr)->Release();
                *_ppComPtr = 0;
            }
        }
#endif /* _TITE_RENDERER_D3D */
    }// End of core namespace
}// End of tite namespace

#endif /* TITE_CORE_SAFE_DELETE_HPP */